package com.service.impl;

import com.dao.ActivityMapper;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.service.ActivityService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    public Object addActivity(Activity activity) {
        ProJects proJects = activity.getProJects();
        List<School> schools = activity.getProJects().getSchool();
        activityMapper.addProJects(proJects);
        System.out.println(proJects);
        int proID = proJects.getId();
        for (Food food:activity.getFoods()){
            food.setProID(proID);
        }
        for (Proplan proplan: activity.getProplans()){
            proplan.setProID(proID);
        }
        for (School school:schools){
            school.setProID(proID);
        }
        activityMapper.addFood(activity.getFoods());
        activityMapper.addProplan(activity.getProplans());
        activityMapper.addSchool(schools);
        Data data = new Data();
        data.setState(true);

        data.setData(JSONObject.fromObject("{ \"proID\":"+proID+"}"));
        return data;
    }
    public Object addFood(Food food){
        Data data = new Data();
        List<Food> list = new ArrayList<Food>();
        list.add(food);
        try {
            activityMapper.addFood(list);
        }catch (Exception e) {
            e.printStackTrace();
            data.setState(false);
            data.setError("添加失败");
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object addProplan(Proplan proplan){
        Data data = new Data();
        List<Proplan> list = new ArrayList<Proplan>();
        list.add(proplan);
        try {
            activityMapper.addProplan(list);
        }catch (Exception e) {
            e.printStackTrace();
            data.setState(false);
            data.setError("添加失败");
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object deleteFood(int foodID){
        Data data = new Data();
        try {
            activityMapper.deleteFood(foodID);
        }catch (Exception e) {
            e.printStackTrace();
            data.setState(false);
            data.setError("删除失败");
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object deleteProplan(int proplanID){
        Data data = new Data();
        try {
            activityMapper.deleteProplan(proplanID);
        }catch (Exception e) {
            e.printStackTrace();
            data.setState(false);
            data.setError("删除失败");
            return data;
        }
        data.setState(true);
        return data;
    }
    public List<ProJectsPage> selectProJectsPage(Paging paging){
        PageHelper.startPage(paging.getPage(), paging.getLimit());
        List<ProJectsPage>  proJectsPages  = activityMapper.selectProJectsPage();
        for (ProJectsPage proJectsPage :proJectsPages){
            int proID = proJectsPage.getId();
            proJectsPage.setSchools(activityMapper.selectSchool(proID));
        }
        return proJectsPages;
    }
    public List<Food> selectFoodByID(int id){
        return activityMapper.selectFoodByID(id);
    }

    public List<Proplan> selectProplanByID(int id){
        return activityMapper.selectProplanByID(id);
    }

    public int addScenery(Scenery scenery){
        return activityMapper.addScenery(scenery);
    }
    public List<Scenery> selectSceneryByID(int proID){
        return activityMapper.selectSceneryByID(proID);
    }
    public int deleteSceneryByID(Scenery scenery){
        return activityMapper.deleteSceneryByID(scenery);
    }
    public Object queryProJectsPageByName(String activeName){
       List<ProJectsPage>  proJectsPages = activityMapper.selectProJectsPageByName(activeName);
        for (ProJectsPage proJectsPage :proJectsPages){
            int proID = proJectsPage.getId();
            proJectsPage.setSchools(activityMapper.selectSchool(proID));
        }
        return proJectsPages;
    }
    public Object addBase(Base base){
        Data data = new Data();
        try {
            activityMapper.addBase(base);

        }catch (Exception e) {
            data.setState(false);
            data.setError("添加失败");
            e.printStackTrace();
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object selectBases(){
        Data data = new Data();
        try {
            data.setData(activityMapper.selectBases());
        }catch (Exception e) {
            data.setState(false);
            data.setError("查询失败");
            e.printStackTrace();
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object deleteBaseByName(String baseName){
        Data data = new Data();
        try {
           if (activityMapper.deleteBaseByName(baseName)==0){
               data.setState(false);
               data.setError("删除失败！");
               return  data;
           }
        }catch (Exception e) {
            data.setState(false);
            data.setError("删除失败");
            e.printStackTrace();
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object selectBaseByName(String baseName){
        Base base = activityMapper.selectBaseByName(baseName);
        Data data = new Data();
        if (base == null){
            data.setState(true);
            return data;
        }else {
            data.setState(false);
            data.setError("基地以存在！");
            return data;
        }
    }
    public Object addAgency(Agency agency){
        Data data = new Data();
        try {
            if (activityMapper.addAgency(agency) == 0) {
                data.setState(false);
                data.setError("添加失败！");
                return data;
            } else {
                data.setState(true);
                return data;
            }
        }catch (Exception e) {
            data.setState(false);
            data.setError("添加失败！");
            e.printStackTrace();
            return data;
        }
    }
    public Object selectAgencys(){
        Data data = new Data();
        try {

                data.setState(true);
                data.setData(activityMapper.selectAgencys());
                return data;
        }catch (Exception e) {
            data.setState(false);
            data.setError("发生错误！");
            e.printStackTrace();
            return data;
        }
    }
    public Object selectAgency(String travelAgencyValue){
        Agency agency = activityMapper.selectAgency(travelAgencyValue);
        Data data = new Data();
        if (agency == null){
            data.setState(true);
            return data;
        }else {
            data.setState(false);
            data.setError("旅行社已存在！");
            return data;
        }
    }
    public Object deleteAgencyByName(String travelAgencyValue){
        Data data = new Data();
        try {
            if (activityMapper.deleteAgencyByName(travelAgencyValue)==0){
                data.setState(false);
                data.setError("删除失败！");
                return  data;
            }else {
                data.setState(true);
                return data;
            }
        }catch (Exception e) {
            data.setState(false);
            data.setError("删除失败");
            e.printStackTrace();
            return data;
        }

    }
}
